﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：武双 
    //
    // 文件名：BulidPaymentAdd.cs
    // 文件功能描述：结算管理：新增结算单
    // 创建标识：武双2009-05-12
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;


namespace HZ.DrugTrade.Web.Hospitals.Collection
{
    public partial class BulidPaymentManage : BasePage
    {
        int totalDrugrecord = 0;

        public BulidPaymentManage()
        {
            this.ModuleNo = Module.结算单管理.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            QueryData();
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            QueryData();
        }

        protected void QueryData()
        {
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            HZ.DrugTrade.Model.Query.InvoicePay qryWhere = new HZ.DrugTrade.Model.Query.InvoicePay();
            qryWhere.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            //qryWhere.InvoicePayNo = txtProjectNo.Text.Trim();
            qryWhere.InvoicePayName = txtDistriPlanName.Text.Trim();
            qryWhere.CompanyName_PS = lblCompanyName.Text.Trim();
            qryWhere.PayStatus=0;
            //qryWhere.SortFields = "InvoicePayTime DESC";
            IList<Model.View.InvoicePay> lstInvoicePay = new List<Model.View.InvoicePay>();
            IBLL_Purchase iPurchase = Factory.BLL_Purchase();
            lstInvoicePay = iPurchase.GetTotalInvoicePayByWhere(qryWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalDrugrecord);
            gvwProcurementProjectList.DataSource = lstInvoicePay;
            gvwProcurementProjectList.AutoGenerateColumns = false;
            gvwProcurementProjectList.DataBind();
            AspNetPager1.RecordCount = totalDrugrecord;

        }

        protected void gvwProcurementProjectList_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            //修改结算单
            if (e.CommandName.Equals("Add"))
            {
                HZ.DrugTrade.Model.InvoicePay qryWhere = new HZ.DrugTrade.Model.InvoicePay();
                qryWhere.InvoicePaySerialID = e.CommandArgument.ToString();
                IBLL_Purchase iPurchase = Factory.BLL_Purchase();
                int nSuccess = iPurchase.SelectInvoiceState(qryWhere);
                if (nSuccess == 1)
                {
                    HZ.Web.WebUtil.Alert("结算单已确认，无法进行修改!", this);
                    HZ.Web.WebUtil.RedirectByScript("BulidPaymentManage.aspx");
                    return;
                }
                else
                {
                   Response.Redirect("BulidPaymentAdd.aspx?IID='" + qryWhere.InvoicePaySerialID + "'");
                }
            }
            //删除结算单
            if (e.CommandName.Equals("Del"))
            {
                HZ.DrugTrade.Model.InvoicePay qryWhere = new HZ.DrugTrade.Model.InvoicePay();
                qryWhere.InvoicePaySerialID = e.CommandArgument.ToString();
                IBLL_Purchase iPurchase = Factory.BLL_Purchase();
                int state = iPurchase.SelectInvoiceState(qryWhere);
                if (state == 1)
                {
                    HZ.Web.WebUtil.Alert("结算单已确认，无法进行修改!", this);
                    HZ.Web.WebUtil.RedirectByScript("BulidPaymentManage.aspx");
                    return;
                }
                else
                {
                    int nSuccess = iPurchase.DeleteInvoicePay(qryWhere);
                    if (nSuccess == 1)
                    {
                        HZ.Web.WebUtil.Alert("结算单删除成功!", this);
                        HZ.Web.WebUtil.RedirectByScript("BulidPaymentManage.aspx");
                        return;
                    }
                    else
                    {
                        HZ.Web.WebUtil.Alert("结算单删除失败!", this);
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 绑定结算单状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProcurementProjectList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblPayStatus = e.Row.FindControl("lblPayStatus") as Label;
                Label lblMod = e.Row.FindControl("lblMod") as Label;
                LinkButton lbnDel = e.Row.FindControl("lbtnDel") as LinkButton;
                if (lblPayStatus.Text == "0")
                {
                    lblPayStatus.Text = "未确认";
                }
                else if (lblPayStatus.Text == "1")
                {
                    //已确认的结算单不具有修改和删除功能
                    lblPayStatus.Text = "已确认";
                    lblMod.Visible = false;
                    lbnDel.Visible = false;
                }
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            HZ.DrugTrade.Model.Query.InvoicePay qryWhere = new HZ.DrugTrade.Model.Query.InvoicePay();
            qryWhere.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            //qryWhere.InvoicePayNo = txtProjectNo.Text.Trim();
            qryWhere.InvoicePayName = txtDistriPlanName.Text.Trim();
            qryWhere.CompanyName_PS = lblCompanyName.Text.Trim();
            qryWhere.PayStatus = 0;
            qryWhere.SortFields = "InvoicePayTime DESC";
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.InvoicePay>(qryWhere, true);
            string sql = string.Format(@"SELECT DISTINCT  CompanyID_PS 配送企业编号
                                            ,CompanyName_PS 配送企业名称
                                            ,InvoicePayTime 结算时间
                                            ,PayAmount 应付金额
                                            ,ActualAmount 实付金额
                                            ,InvoicePayName 结算单名称
                                            ,HospitalName 医疗机构名称
                                            ,AreaID 地区编号
                                            ,HospitalID 医疗机构编号
                                            ,Remarks 备注 FROM View_InvoicePay  WHERE InvoicePaySerialID IS NOT NULL {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "结算单信息.xls");
        }
    }
}